let filesNum = 0;
$(document).ready(function () {
    pickout.to({
        el: '.city',
        theme: 'dark',
        search: true
    });

    pickout.to({
        el: '.state',
        theme: 'clean',
    });

    pickout.updated('.city');
});

$("#auditButton").on('click', function (e) {
    if ($('#uploaderInput').get(0) != undefined) {
        $("#uploaderInput").remove();
        filesNum -= 1;
    }
    let formData = new FormData();
    //获取所有图片
    for (let i = 0; i < filesNum; i++) {
        let classStr = ".files" + i;
        for (let j = 0; j < $(classStr)[0].files.length; j++) {
            let file = $(classStr)[0].files[j];
            formData.append('files', file);
        }
    }
    formData.append('healthManageId', $("#healthManageSelect").val());
    formData.append('doctorId', $("#doctorSelect").val());
    WeKnow.loading();
    $.ajax({
        url: "takePicture/uploadPrescription",														//后台接收数据地址
        data: formData,
        type: "POST",
        dataType: "json",
        cache: false,//上传文件无需缓存
        processData: false,//用于对data参数进行序列化处理 这里必须false
        contentType: false,
        success: function (data) {
            WeKnow.closeLoading();
            if (data.errCode === 0) {
                WeKnow.successMsg(data.errMsg);
                //2秒后跳转到首页
                setInterval(function () {
                    WeKnow.location("login");
                    WeKnow.closeLoading();
                }, 2000);
            } else {
                if (data.errCode === -2) {
                    //确认框
                    WeKnow.confirm(data.errMsg, function () {


                    });
                } else {
                    WeKnow.errorMsg(data.errMsg);
                }

            }
        }
    });
});

$(".picDiv").on("click", function () {
    if ($('#uploaderInput').get(0) != undefined) {
        $("#uploaderInput").remove();
        filesNum -= 1;
    }
    var uploadFile = '<input style="display: none" class="files' + filesNum + '" id="uploaderInput"  type="file" accept="image/*"/>';
    $("#fileDiv").append($(uploadFile));
    $("#uploaderInput").bind("change", function (e) {
        //可以做一些其他的事，比如图片预览
        showPicture(e.currentTarget);
        $(this).removeAttr("id");
    });
    $("#uploaderInput").click();
    filesNum += 1;
});

$('#uploadButton').on('click', function () {
    $('#appAddPrescriptionTemplateDiv').modal('show');
});

//保存处方模板
$('#appSavePrescriptionTemplate').on('click', function () {

    let templateName = $.trim($("#appTemplateName").val());//模板名称
    if (!templateName) {
        WeKnow.notifyMsg("请输入模板名称")
        return null;
    }

    if (!$(".files0")[0]) {
        WeKnow.notifyMsg("请上传一张处方模板")
        return null;
    }
    let file = $(".files0")[0].files[0];
    let formData = new FormData();
    formData.append('templateName', templateName);
    formData.append('healthManageId', $("#healthManageSelect").val());
    formData.append('file', file);
    formData.set("flg", "0");
    WeKnow.loading();
    $.ajax({
        url: "takePicture/appSavePrescriptionTemplate",														//后台接收数据地址
        data: formData,
        type: "POST",
        dataType: "json",
        cache: false,//上传文件无需缓存
        processData: false,//用于对data参数进行序列化处理 这里必须false
        contentType: false,
        success: function (data) {
            WeKnow.closeLoading();
            if (data.errCode === 0) {
                $('#appAddPrescriptionTemplateDiv').modal("hide");
                WeKnow.successMsg("模板已提交，等待技术人员配置！");
            } else {
                if (data.errCode === -2) {
                    //确认框
                    WeKnow.confirm(data.errMsg, function () {
                        WeKnow.loading();
                        formData.set("flg", "1");
                        $.ajax({
                            url: "takePicture/appSavePrescriptionTemplate",														//后台接收数据地址
                            data: formData,
                            type: "POST",
                            dataType: "json",
                            cache: false,//上传文件无需缓存
                            processData: false,//用于对data参数进行序列化处理 这里必须false
                            contentType: false,
                            success: function (data) {
                                WeKnow.closeLoading();
                                if (data.errCode === 0) {
                                    $('#appAddPrescriptionTemplateDiv').modal("hide");
                                    WeKnow.successMsg("模板已提交，等待技术人员配置！");
                                }
                            }
                        });

                    });
                } else {
                    WeKnow.errorMsg(data.errMsg);
                }
            }
        }
    });
});

//添加图片
function showPicture(eleobj) {
    for (let i = 0; i < eleobj.files.length; i++) {
        let imgurl = window.URL.createObjectURL(eleobj.files[i]);
        let html = '<div  class="uploadDiv"> ' +
            '<img class="uploadPic" src="' + imgurl + '" data-preview-src="" data-preview-group="1"/> ' +
            '<img class="delPic" src="img/del.png"   onclick="removeshow(this,' + (filesNum - 1) + ')" />' +
            '</div>';
        $(".picInput").parent('div').before(html);
    }
    ;
    mui.previewImage();
}

//删除图片
function removeshow(eleobj, index) {
    let classStr = ".files" + index;
    $(eleobj).parent().remove();
    $(classStr).remove();
    filesNum -= 1;
}


function getDoctorByInstitutionId() {
    let params = {}
    params.institutionId = $("#healthManageSelect").val();
    //请求接口
    $.ajax({
        type: "POST",
        url: "takePicture/getDoctorByInstitutionId",
        dataType: "json",
        data: params,
        async: true,//异步调用
        success: function (data) {
            WeKnow.closeLoading();
            if (data.errCode == 1) {
                //重新生成页面下拉
                let doctorSelect = $('#doctorSelect');
                doctorSelect.empty();
                $("#textarea1").val("");
                let dataInfo = data.doctorList;
                if (dataInfo && dataInfo.length) {
                    let e = '';
                    $(dataInfo).each(function (idx, rowData) {
                        if (idx == 0) {
                            $("#textarea1").val(rowData.name);
                            e += '<option selected value="' + rowData.id + '">' + rowData.name + '</option>';
                        } else {
                            e += '<option value="' + rowData.id + '">' + rowData.name + '</option>';
                        }

                    })
                    doctorSelect.append(e);
                }

            } else if (data.errCode == -1) {
                window.parent.WeKnow.location("loginTimeout");
            } else {
                WeKnow.errorMsg(data.errMsg);
            }
        }
    });
}